local MakePlayerCharacter = require("prefabs/player_common")

--local easing = require("easing")
local assets = {
	Asset("SCRIPT", "scripts/prefabs/player_common.lua"),
	Asset("ANIM", "anim/player_basic.zip"),
	Asset("ANIM", "anim/player_idles_shiver.zip"),
	Asset("ANIM", "anim/player_actions.zip"),
	Asset("ANIM", "anim/player_actions_axe.zip"),
	Asset("ANIM", "anim/player_actions_pickaxe.zip"),
	Asset("ANIM", "anim/player_actions_shovel.zip"),
	Asset("ANIM", "anim/player_actions_blowdart.zip"),
	Asset("ANIM", "anim/player_actions_eat.zip"),
	Asset("ANIM", "anim/player_actions_item.zip"),
	Asset("ANIM", "anim/player_actions_uniqueitem.zip"),
	Asset("ANIM", "anim/musha_actions_uniqueitem.zip"),
	Asset("ANIM", "anim/musha_actions_speargun.zip"),
	Asset("ANIM", "anim/musha_actions_telescope.zip"),
	Asset("ANIM", "anim/player_actions_bugnet.zip"),
	Asset("ANIM", "anim/player_actions_fishing.zip"),
	Asset("ANIM", "anim/player_actions_boomerang.zip"),
	Asset("ANIM", "anim/player_bush_hat.zip"),
	Asset("ANIM", "anim/player_attacks.zip"),
	Asset("ANIM", "anim/player_idles.zip"),
	Asset("ANIM", "anim/player_rebirth.zip"),
	Asset("ANIM", "anim/player_jump.zip"),
	Asset("ANIM", "anim/player_amulet_resurrect.zip"),
	Asset("ANIM", "anim/player_teleport.zip"),
	Asset("ANIM", "anim/wilson_fx.zip"),
	Asset("ANIM", "anim/player_one_man_band.zip"),
	Asset("ANIM", "anim/shadow_hands.zip"),
	Asset("SOUND", "sound/sfx.fsb"),
	Asset("SOUND", "sound/wilson.fsb"),
	Asset("ANIM", "anim/beard.zip"),
	Asset("ANIM", "anim/musha_body_purple.zip"),
	Asset("ANIM", "anim/musha.zip"),
	Asset("ANIM", "anim/musha_body_white.zip"),
	Asset("ANIM", "anim/musha_ghost_build.zip")
}

local prefabs = {
	"book_birds",
	"book_tentacles",
	"book_gardening",
	"book_sleep",
	"book_brimstone"
}

local start_inv = {
	"musha_glowdust",
	"musha_glowdust"
}

--[[ local function DoEffects(pet)
	local x, y, z = pet.Transform:GetWorldPosition()
	SpawnPrefab("statue_transition_2").Transform:SetPosition(x, y, z)
end

local function KillPet(pet)
	pet.components.health:Kill()
end

function OnSpawnPet(inst, pet)
	if pet:HasTag("shadowminion") then
		--Delayed in case we need to relocate for migration spawning
		pet:DoTaskInTime(0, DoEffects)

		if not (inst.components.health:IsDead() or inst:HasTag("playerghost")) then
			inst.components.sanity:AddSanityPenalty(pet, TUNING.SHADOWWAXWELL_SANITY_PENALTY[string.upper(pet.prefab)])
			inst:ListenForEvent("onremove", inst._onpetlost, pet)
		elseif pet._killtask == nil then
			pet._killtask = pet:DoTaskInTime(math.random(), KillPet)
		end
	elseif inst._OnSpawnPet ~= nil then
		inst:_OnSpawnPet(pet)
	end
end

function OnDespawnPet(inst, pet)
	if pet:HasTag("shadowminion") then
		DoEffects(pet)
		pet:Remove()
	elseif inst._OnDespawnPet ~= nil then
		inst:_OnDespawnPet(pet)
	end
end ]]
local function change_action_speed(inst)
	musha_print("onEntityReplicated")
	local getactionspeed = function()
		return 0.4 * inst.replica.musha_staminasleep:GetPercent() + 0.6 * (1 - inst.replica.musha_fatiguesleep:GetPercent())
	end
	inst.sg.sg.actionhandlers[ACTIONS.BUILD].deststate = function(inst)
		return getactionspeed() > 0.65 and "domediumaction" or "dolongaction"
	end
	inst.sg.sg.actionhandlers[ACTIONS.HARVEST].deststate = function(inst)
		return getactionspeed() > 0.45 and "domediumaction" or "dolongaction"
	end
	inst.sg.sg.actionhandlers[ACTIONS.PICK].deststate = function(inst)
		return (getactionspeed() > 0.6 and "doshortaction") or (getactionspeed() > 0.3 and "domediumaction") or "dolongaction"
	end
end

local function common_postinit(inst)
	inst.soundsname = "willow"
	inst.MiniMapEntity:SetIcon("musha_mapicon.tex")
	inst.musha_settings = deepcopy(MUSHA_SETTINGS.musha_settings)

	inst:AddTag("bookbuilder") -- book maker
	inst:AddTag("_musha")

	inst.components.talker.fontsize = 26
	inst.components.talker.colour = Vector3(0.75, 0.9, 1, 1)

	inst.entity:AddLight()
	inst.Light:SetRadius(2.5)
	inst.Light:SetFalloff(.8)
	inst.Light:SetIntensity(.8)
	inst.Light:SetColour(150 / 255, 150 / 255, 150 / 255)
	local EnableMovementPrediction_Old = inst.EnableMovementPrediction
	inst.EnableMovementPrediction = function(inst, enable)
		EnableMovementPrediction_Old(inst, enable)
		if USE_MOVEMENT_PREDICTION and not TheWorld.ismastersim and enable and inst.components.locomotor ~= nil then
			change_action_speed(inst)
		end
	end
end

local function master_postinit(inst)
	inst.with_tent = false
	inst.with_fire = false

	inst.components.builder.science_bonus = inst.musha_settings.musha_intelligence
	--[[ 	if inst.components.petleash ~= nil then
		inst._OnSpawnPet = inst.components.petleash.onspawnfn
		inst._OnDespawnPet = inst.components.petleash.ondespawnfn
		inst.components.petleash:SetMaxPets(inst.components.petleash:GetMaxPets() + 4)
	else
		inst:AddComponent("petleash")
		inst.components.petleash:SetMaxPets(4)
	end
	inst.components.petleash:SetOnSpawnFn(OnSpawnPet)
	inst.components.petleash:SetOnDespawnFn(OnDespawnPet)

	inst._onpetlost = function(pet)
		inst.components.sanity:RemoveSanityPenalty(pet)
	end ]]
	-----------
	inst:AddComponent("musha_skills")
	inst:AddComponent("musha_leader")
	inst:AddComponent("musha_fatiguesleep") --动作
	inst:AddComponent("musha_staminasleep") --移动
	inst:AddComponent("musha_equipbuff")
	inst:AddComponent("reader")
	--------------------------
	inst.components.health:SetMaxHealth(80)
	inst.components.sanity:SetMax(80)
	inst.components.hunger:SetMax(200)

	inst.components.temperature.hurtrate = 0.5

	inst.components.eater.stale_hunger = TUNING.WICKERBOTTOM_STALE_FOOD_HUNGER
	inst.components.eater.stale_health = TUNING.WICKERBOTTOM_STALE_FOOD_HEALTH
	inst.components.eater.spoiled_hunger = TUNING.WICKERBOTTOM_SPOILED_FOOD_HUNGER
	inst.components.eater.spoiled_health = TUNING.WICKERBOTTOM_SPOILED_FOOD_HEALTH

	inst.talksoundoverride = nil
	inst.hurtsoundoverride = nil

	change_action_speed(inst)

	return inst
end

return MakePlayerCharacter("musha", prefabs, assets, common_postinit, master_postinit, start_inv)
